<template>
	<el-dialog :visible.sync="addPointShow" @close="$emit('update:showAddpoint',false)" width="55%" :show-close='false' :append-to-body='true'>
		<span slot="title" class="dialog-title">
		  	添加QC
			<el-button  size="mini" class="popupClose" @click="resetPointForm()">关闭</el-button>
			  </span>
		<!--{{sampleForm}}-->
		<el-form label-width="110px" :inline="false" class="demo-form-inline" :model="sampleForm" :rules="sampleRules" ref="sampleForm">
			<el-form-item label="质控类型">
				<el-col :span="22">
					<el-radio-group v-model="sampleForm.qcType">
						<el-radio v-for="qc in QCS" :label="qc.code" :key="qc.id" :disabled="selectedArr.length==0">{{qc.name}}</el-radio>
					</el-radio-group>
					<br />
					<el-radio-group v-model="sampleForm.qcType">
						<el-radio v-for="qc in QCS1" :label="qc.code" :key="qc.id" :disabled="selectedArr.length!=0">{{qc.name}}</el-radio>
					</el-radio-group>

				</el-col>
			</el-form-item>
		</el-form>
		<span slot="footer" class="dialog-footer">
			    <el-button type="primary" @click="addSubmit" size="mini">确 定</el-button>
			    <el-button  size="mini" @click="resetPointForm()">取 消</el-button>
			  </span>
	</el-dialog>
</template>

<script>
	import { mapState } from 'vuex'
	export default {
		props: ['showAddpoint', 'selectedArr', 'sampleData', 'sampleForm', 'currentArr'],
		data() {
			return {
				checkedQC: [],
				newOrEdit: 'new',
				sampleRules: {},
				typeOptions: [],
				monitorTypeArr: [],
				qcValue: false,
			}
		},
		computed: {
			...mapState(['typeQcSelect', 'QCS', 'QCS1']),
			addPointShow: {
				get: function () {
					return this.showAddpoint
				},
				set: function (newValue) {

				}
			},
			//			QCS(){
			//				return this.$store.state.typeQcSelect.itemList.filter(item => item.genre===2 )
			//			},
			//			QCS1(){
			//				return this.$store.state.typeQcSelect.itemList.filter(item => item.genre===1 )
			//			}
		},
		mounted() {
			this.$store.dispatch('dogetDictionary')
		},
		methods: {
			getQcName(code) {
				this.typeQcSelect.itemList = [...this.QCS, ...this.QCS1]
				return this.typeQcSelect.itemList.filter(item => item.code === code)
			},
			resetPointForm() {
				this.$emit('update:showAddpoint', false)
			},
			generateMixed(n) {
				var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
				var res = "";
				for (var i = 0; i < n; i++) {
					var id = Math.ceil(Math.random() * 35);
					res += chars[id];
				}
				return res;
			},
			addSubmit() {
				if (this.sampleForm.qcType == undefined) {
					this.$notify({
						title: "警告",
						message: "请选先选中QC！",
						type: "warning",
					})
					return;
				}
				let addArr = [];
				let count = 1;
				if (this.selectedArr.length == 0) {
					this.sampleForm.name = this.getQcName(this.sampleForm.qcType)[0].name;
					this.sampleForm.parent = this.generateMixed(32);
					console.log(this.currentArr)
					// if (this.currentArr.name != undefined) {
					// 	this.sampleForm.name = this.currentArr.monitorName + '-' + this.getQcName(this.sampleForm.qcType)[0].name;
					// } else {
            this.sampleForm.group = 2; //无父样
						this.sampleForm.name = this.getQcName(this.sampleForm.qcType)[0].name;
					// }
					// this.sampleForm.monitor = this.currentArr.monitor
					// this.sampleForm.monitorName = this.currentArr.monitorName
					// this.sampleForm.monitorZone = this.currentArr.monitorZone
					// this.sampleForm.monitorZoneName = this.currentArr.monitorZoneName
					this.sampleData.unshift(this.sampleForm)
				} else {

					for (let i = 0; i < this.sampleData.length; i++) {
						for (let j = 0; j < this.selectedArr.length; j++) {
							if (this.sampleData[i].parent == this.selectedArr[j].parent && !this.sampleData[i].qcType && !this.selectedArr[j].qcType) {


								let item = {};
								let nextAddPoint;
								nextAddPoint = Object.assign({}, this.selectedArr[j])
								nextAddPoint.name = this.selectedArr[j].name + '-' + this.getQcName(this.sampleForm.qcType)[0].name;
								nextAddPoint.qcType = this.sampleForm.qcType
								nextAddPoint.group = 1; //有父样
								item.index = i + count;
								item.arry = nextAddPoint;
								addArr.push(item)
								count++

							}
						}
					}

					for (let item of addArr) {
						this.sampleData.splice(item.index, 0, item.arry)
					}
				}
				this.$parent.$refs.sampleTable.clearSelection();
				this.$parent.$refs.sampleTable.setCurrentRow();
				this.$parent.currentArr = {};
				this.resetPointForm()
			}
		}
	}

</script>

<style>

</style>
